<template>
  <div class="container">
    <div>
      <div>
        <el-form ref="dataForm" :model="record" label-position="top" size="small"  label-width="110px">
          <el-row>
            <el-col>
              <span class="customSpan">流程分类：{{record.bizCateName}}</span>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-upload
                class="upload-demo"
                ref="upload"
                list-type="picture"
                :headers="fileHeaders"
                :name="fileUploadName"
                :action="fileUploadUrl"
                :multiple="false"
                :auto-upload="false"
                :file-list="fileList"
                :limit="fileLimit"
                :accept="fileAccept"
                :on-exceed="handleExceed"
                :before-upload="handleBeforeUpload"
                :on-change="handleChange"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
                :on-error="handleError"
                :data="record"
              >
                <el-button slot="trigger" size="mini" type="primary">选取文件</el-button>
                <el-button size="mini" type="success" @click="createSubmit('dataForm')">导入流程<i class="el-icon-upload el-icon--right"></i></el-button>
              </el-upload>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
  import { getToken } from '@/utils/auth'

  export default {
    name: 'wfProcessImport',
    data() {
      return {
        fileHeaders: {
          Authorization: 'bearer ' + getToken()
          // 'Content-Type': 'multipart/form-data'
        },
        fileUploadUrl: process.env.BASE_API + '/workflow' + '/WfProcess/doImportProcess',
        fileUploadName: 'processFile',
        fileList: [],
        fileLimit: 1,
        fileAccept: '.ipdl',
        record: {
          bizCateId: '',
          bizCateName: ''
        }
      }
    },
    props: ['bizCateId', 'bizCateName'],
    created() {
      this.record.bizCateId = this.bizCateId
      this.record.bizCateName = this.bizCateName
    },
    watch: {
      // 如果路由有变化，会再次执行该方法
      // '$route.name': ['GetCodeList', 'GetCodeList1']
    },
    components: {
    },
    methods: {
      handleChange(file, fileList) {
        this.fileList = fileList
      },
      handleRemove(file, fileList) {
        // console.log('this.fileList', this.fileList)
        // console.log('file', file)
        // console.log('fileList', fileList)
        this.fileList = fileList
      },
      handleExceed(files, fileList) { // 文件超出上传个数时
        let tempFile = files[0]
        tempFile.status = 'ready'
        this.fileList = []
        this.fileList.push(tempFile)
      },
      handleBeforeUpload(file) {
      },
      handleSuccess(response) {
        this.$refs.upload.clearFiles()
        if (response.success) {
          this.$message({
            type: 'success',
            message: '导入成功!'
          })
          this.$emit('doComplete', {})
        } else {
          this.$message({
            type: 'error',
            message: '导入失败!' + response.message
          })
        }
      },
      handleError(file, fileList) {
        this.fileList = []
        this.$message({
          type: 'error',
          message: '导入失败!'
        })
      },
      createSubmit(formName) {
        this.submitUpload()
      },
      submitUpload() {
        if (this.fileList == null || this.fileList.length <= 0) {
          this.$message({
            type: 'error',
            message: '请选择导入的流程!'
          })
          return false
        }
        this.$refs.upload.submit()
      },
      cancelSubmit() {
        //
      }
    }
  }
</script>
<style scoped>
  .menu-navbar{
    padding:0 0 20px;
    text-align: right;
  }
  .container{
    /*padding: 20px;*/
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .customSpan{
    font-weight: bold;
    font-size: 16px;
    color: #333333;
    display: inline-block;
    vertical-align: middle;
    line-height: 32px;
    padding-left: 3px;
    padding-bottom: 10px;
  }
</style>
